package com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetailUp;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetail.Advice;
import com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetail.CasesDetail;
import com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetail.Quslist;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.FileResourceClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.ImageInfo;
import com.dididoctor.patient.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： CasesDetailUpPresenter
 * @包名称： com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetailUp
 * @创建人： zhanjunjie
 * @创建时间：2016/4/15 16:37
 * @修改人：
 * @修改时间：2016/4/15 16:37
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class CasesDetailUpPresenter extends BasePresenter {

    private CasesDetailUpView view;

    public CasesDetailUpPresenter(Context context, CasesDetailUpView view) {
        super(context, view);
        this.view = view;
    }

    /**
     * 获取电子病历详情
     *
     * @param recId
     */
    public void getcasesdetail(String recId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("recId", recId);
        BusinessClient.post(ConstantValue.medicaldetail, params, new MyHttpResponseHandler() {

            private List<Map<String, String>> quslistsmaps = new ArrayList<Map<String, String>>();
            private List<Map<String, String>> advicesmaps = new ArrayList<Map<String, String>>();
            private List<Quslist> quslists = new ArrayList<Quslist>();
            private List<ImageInfo> imageInfos = new ArrayList<ImageInfo>();
            private List<Advice> advices = new ArrayList<Advice>();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    CasesDetail casesDetail = new CasesDetail();
                    casesDetail.setRecId(Util.toString(response.getString("recId")));
                    casesDetail.setHosName(Util.toString(response.getString("hosName")));
                    casesDetail.setClinicDate(Util.toString(response.getString("clinicDate")));
                    casesDetail.setDctName(Util.toString(response.getString("dctName")));
                    casesDetail.setDescr(Util.toString(response.getString("descr")));
                    casesDetail.setResult(Util.toString(response.getString("result")));
                    //图片
                    final Object[] piclists = response.getArgs("piclist");
                    for (int i = 0; i < piclists.length; i++) {
                        ImageInfo imageInfo = new ImageInfo();
                        imageInfo.setKey(Util.toString(piclists[i]));
                        imageInfo.setUrl(Util.toString(piclists[i]));
                        imageInfo.setType(3);
                        imageInfos.add(imageInfo);
                    }
                    //问诊表
                    quslistsmaps = response.getListData("quslist");
                    for (Map<String, String> map : quslistsmaps) {
                        Quslist quslist = new Quslist();
                        quslist.setContent(Util.toString(map.get("content")));
                        quslist.setAnswer(Util.toString(map.get("answer")));
                        quslist.setQusId(Util.toString(map.get("qusId")));
                        quslist.setType(Util.toString(map.get("type")));
                        quslists.add(quslist);
                    }
                    //医生建议
                    advicesmaps = response.getListData("advices");
                    for (Map<String, String> map : advicesmaps) {
                        Advice advice = new Advice();
                        advice.setType(Util.toString(map.get("type")));
                        advice.setContent(Util.toString(map.get("content")));
                        advices.add(advice);
                    }
                    view.getcasesdetail(casesDetail, quslists, advices, imageInfos);
                } else {
                    view.showToastMessage(response.getMsg());
                    view.getcasesdetailfail();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getcasesdetailfail();
            }
        });
    }

    /**
     * @param hospital
     * @param clinicDate
     * @param doctorName
     * @param descr
     * @param result
     * @param advices
     * @param pics
     * @param recId      修改时使用
     */
    public void uploadCases(String hospital, String clinicDate, String doctorName, String descr, String result, String advices, String pics, String recId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("hospital", hospital);
        params.put("clinicDate", clinicDate);
        params.put("doctorName", doctorName);
        params.put("descr", descr);
        params.put("result", result);
        params.put("advices", advices);
        params.put("pics", pics);
        params.put("recId", recId);
        BusinessClient.post(ConstantValue.submitmdcrecord, params, new MyHttpResponseHandler() {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.uploadsucced();
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.uploadfail();
            }
        });

    }

    /**
     * 上传图片
     *
     * @param path
     */
    public void updateImageFromUri(String path) {

        FileResourceClient.uploadToAppService(context, new MyHttpResponseHandler(view) {

            @Override
            public void onMyResponse(Response response) {
                if (!response.isSucceed()) {
                    view.showToastMessage("消息发送失败." + response.getMsg());
                    return;
                }
                String imageKey = response.getString("picUrl");
                ImageInfo imageInfo = new ImageInfo();
                imageInfo.setId(System.currentTimeMillis());
                imageInfo.setUrl(imageKey);
                imageInfo.setType(3);
                imageInfo.setIds(response.getString("ids"));
                imageInfo.setKey(imageKey);
                view.addImage(imageInfo);
            }

            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                view.onProgress(bytesWritten, totalSize);
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
                view.onProgress(100, 100);
                view.showToastMessage("图片发送失败");
            }
        }, path);
    }

    public void deletePic(String picUrl, final int position) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("picUrl", picUrl);
        BusinessClient.post(ConstantValue.dltmdcpic, params, new MyHttpResponseHandler() {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.deletesucced(position);
                } else {
                    view.deletefail();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.deletefail();
            }
        });
    }
}
